package qsl.app.WifiManager;

import qsl.utils.number;
import qsl.utils.string;
import android.app.Activity;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.Spanned;
import android.view.View;
import android.view.Window;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.SwitchButton;

public class ConfigActivity extends Activity {
	
	SwitchButton switchUseStaticIp, switchProxy;
	LinearLayout layout_static_ip, layout_proxy;
	EditText net_ip, net_getway, net_mask, net_dns1, net_dns2, net_proxy_address, net_proxy_port;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.wifi_configer);
		
		Bundle bundle = getIntent().getExtras();
		setTitle(bundle.getString("ssid"));
		
		switchUseStaticIp = (SwitchButton)findViewById(R.id.switchUseStaticIp);
		switchProxy = (SwitchButton)findViewById(R.id.switchProxy);
		
		layout_static_ip = (LinearLayout)findViewById(R.id.layout_static_ip);
		layout_proxy = (LinearLayout)findViewById(R.id.layout_proxy);
		
		net_ip = (EditText)findViewById(R.id.net_ip);
		net_getway = (EditText)findViewById(R.id.net_getway);
		net_mask = (EditText)findViewById(R.id.net_mask);
		net_dns1 = (EditText)findViewById(R.id.net_dns1);
		net_dns2 = (EditText)findViewById(R.id.net_dns2);
		net_proxy_address = (EditText)findViewById(R.id.net_proxy_address);
		net_proxy_port = (EditText)findViewById(R.id.net_proxy_port);
		
		switchUseStaticIp.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton button, boolean checked) {
				layout_static_ip.setVisibility(checked ? View.VISIBLE : View.GONE);
			}
		});
		switchProxy.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton button, boolean checked) {
				layout_proxy.setVisibility(checked ? View.VISIBLE : View.GONE);
			}
		});
	    
	    net_ip.setFilters(new InputFilter[] { string.getIpFilter() });
	    net_getway.setFilters(new InputFilter[] { string.getIpFilter() });
	    net_mask.setFilters(new InputFilter[] { string.getIpFilter() });
	    net_dns1.setFilters(new InputFilter[] { string.getIpFilter() });
	    net_dns2.setFilters(new InputFilter[] { string.getIpFilter() });
	    net_proxy_address.setFilters(new InputFilter[] { string.getIpFilter() });
	    net_proxy_port.setFilters(new InputFilter[] { new InputFilter() {
	    	@Override
	        public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
	            if (end > start) {
	                if(!number.betweenEqual(Integer.valueOf("0" + dest.toString(), 10), 1, 65534)) {
	                	return "";
	                }
	            }
	            return null;
	        }
	    } });
	    
	    switchUseStaticIp.setChecked(true);
	}
}
